/**********************************************************************************************************************
 * \file Cpu0_Main.c
 * \copyright Copyright (C) Infineon Technologies AG 2019
 * 
 * Use of this file is subject to the terms of use agreed between (i) you or the company in which ordinary course of 
 * business you are acting and (ii) Infineon Technologies AG or its licensees. If and as long as no such terms of use
 * are agreed, use of this file is subject to following:
 * 
 * Boost Software License - Version 1.0 - August 17th, 2003
 * 
 * Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and 
 * accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute,
 * and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the
 * Software is furnished to do so, all subject to the following:
 * 
 * The copyright notices in the Software and this entire statement, including the above license grant, this restriction
 * and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all 
 * derivative works of the Software, unless such copies or derivative works are solely in the form of 
 * machine-executable object code generated by a source language processor.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE 
 * COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE.
 *********************************************************************************************************************/
 /*\title Interrupt generation via STM trigger
 * \abstract The STM is configured to trigger an interrupt every 500 ms. The interrupt toggles an LED.
 * \description The System Timer (STM) module counts up to a configured compare value, when it reaches the specific
 *              value, it triggers an interrupt and resets its counter value. The value is set to trigger the interrupt
 *              every 500 ms; at each interrupt an LED is toggled.
 *
 * \name STM_Interrupt_1_KIT_TC387_TFT
 * \version V2.0.0
 * \board APPLICATION KIT TC3X7 V2.0, KIT_A2G_TC387_5V_TFT, TC38xQP_A-Step
 * \keywords AURIX, Interrupt, LED, STM, STM_Interrupt_1, blink
 * \documents README.MD
 * \documents https://www.infineon.com/aurix-expert-training/TC38A_iLLD_UM_1_0_1_17_0.chm
 * \lastUpdated 2024-04-23
 *********************************************************************************************************************/
#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"
#include "os_user.h"
#include "drv_src.h"
#include "srv_uart.h"
#include "port.h"
#include "Echo.h"
#include "Ifx_Lwip.h"
#include "drv_eth.h"
#include "Ifx_Netif.h"
IFX_ALIGN(4) IfxCpu_syncEvent g_cpuSyncEvent = 0;
int test_trap = 0;
void core0_main(void)
{
    IfxCpu_enableInterrupts();
    
    /* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
     * Enable the watchdogs and service them periodically if it is required
     */
    IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
    IfxScuWdt_disableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());
    
    /* Wait for CPU sync event */
    IfxCpu_emitEvent(&g_cpuSyncEvent);
    IfxCpu_waitEvent(&g_cpuSyncEvent, 1);

    init_queue();//关系到异步uart，必须最先初始化
    bsp_bord_init();
    echoInit();
#if !NO_SYS
    freeRTOS_init();
#endif
    while(1)
    {
#if NO_SYS
//        Ifx_Lwip_pollTimerFlags();                          /* Poll LwIP timers and trigger protocols execution if required */
        extern Ifx_Lwip    g_Lwip;
        ifx_netif_input(&g_Lwip.netif);//负责接收以太网中断信号，进入上层协议
#endif
    }
}

int www = 0;
void STM0_ISR()
{
    static int x = 0,y = 0,z = 0;
    x++;y++;z++;
    if(x>100)
    {
        x = 0;
        drv_geth_ctl_status();//如果内部存在uart输出，因此不宜小于100ms周期
    }

#if NO_SYS
    g_TickCount_1ms++;                                      /* Increase LwIP system time                                    */
    static int cnt = 0;
    cnt++;
    Ifx_Lwip_onTimerTick();
    if(cnt>=1000)
    {
        cnt = 0;
        drv_led0_toggle();
    }
#endif
}

